Fix rootdir conftest fixtures not visible to items collected outside the rootdir#14694
Conversation
…#14683) A conftest located in the rootdir used to get an empty baseid (matching every collected item), so its fixtures were visible session-wide. The node-based scoping introduced in 46478fa (pytest-dev#14098, pytest 9.1) scoped it to its own Directory node instead. When --rootdir points to a subdirectory and tests/doctests are collected from a parent directory, that Directory is not an ancestor of the collected items, so the conftest fixtures -- including doctest_namespace injections -- became invisible (NameError in doctests). Restore the pre-9.1 behavior by attaching the rootdir conftest to the Session node, in both places a conftest is parsed: the initial-conftest flush and the Directory collect report. This is behavior-identical for normal layouts (the Session is an ancestor of all items under the rootdir), preserves the pytest-dev#14004 sibling-leak fix, and keeps fixture overrides working (a subdir conftest is still more specific than the Session).
|
Heads up on the two red CI jobs — both are a known repo-wide infra issue, unrelated to this change:
For confirmation, another currently-open PR (#14693, unrelated) fails the identical two jobs with the same cause. All other jobs on this PR (macOS/Ubuntu, Python 3.10–3.15, xdist, plugins, doctesting, etc.) are green. |
f97be8b to
ca7b289
Compare
…ce-not-loaded-outside-rootdir # Conflicts: # testing/test_conftest.py
69782a0 to
3258779
Compare
|
reopend to trigger flaky ci/codecov |
|
Hello, I was wondering, if I would be able to take up commit access to pytest-dev. Happy to keep going through the usual review process for my own PRs and to start helping others. Best regards, |
|
that privilege and responsibility is not something to ask for after one pr merged and one pr in the pipeline it is something we propose to individuals after a trusting relationship has been built - and not all of those take it up |
|
Ah sorry, I missunderstood the Contributor guidelines. Thanks for clarifying. Looking forward to build that relationship and continue building |
Problem
Fixes #14683.
doctest_namespaceinjections (and any fixture defined in the rootdir conftest) stopped being available in pytest 9.1 when--rootdirpoints to a subdirectory and tests/doctests are collected from a parent directory. Doctests then fail withNameError, e.g. the reporter's invocation:Reproduced: 9.0.3 passes, 9.1.1 fails with
NameError: name '...' is not defined.Cause
Bisected to
46478fad5(#14098, pytest 9.1), which changed conftest fixture visibility from nodeid-based to node-based:baseid(relative to rootdir), which matches every collected item — its fixtures were visible session-wide.Directorynode. When items are collected from outside the rootdir, thatDirectoryis a sibling subtree, not an ancestor of the items, so the fixtures are no longer visible.Fix
Restore the pre-9.1 behavior by attaching the rootdir conftest to the
Sessionnode, in the two places a conftest gets parsed:_flush_pending_conftests_to_session— also flush the initial rootdir conftest (e.g. when it is the--config-file) to theSession.pytest_make_collect_report— when collecting the rootdirDirectory, attach its conftest to theSessioninstead of theDirectory.This is safe:
Sessionis an ancestor of all items, so visibility is unchanged.Directory, so sibling-leak does not return.Sessionperis_visibility_more_specific, so it still wins.Tests
testing/test_conftest.py::test_rootdir_conftest_visible_outside_rootdirtesting/test_doctest.py::TestDoctestNamespaceFixture::test_namespace_fixture_from_rootdir_when_modules_outside_rootdir(mirrors the reporter's exact--config-file+ collect-from-parent pattern)Both fail on
mainand pass with this change. Added news fragmentchangelog/14683.bugfix.rst.Note on #14635
#14635 (Home Assistant, "fixtures not found when a parent directory appears multiple times in an argument list") is tracked as a duplicate and is being worked on separately by @RonnyPfannschmidt (multiple
Directorynodes for the same path). This PR targets the rootdir-conftest visibility facet (#14683) only; making the rootdir conftestSession-visible also makes it more robust against the multi-node scenario, but it is not meant to be the complete fix for #14635.